package com.wewins.fota.controller;

import com.wewins.fota.bean.bo.filter.PlanFilterCond;
import com.wewins.fota.bean.dto.PageReq;
import com.wewins.fota.bean.dto.PageRst;
import com.wewins.fota.bean.dto.ResponseRst;
import com.wewins.fota.bean.dto.UpdatePlanDetails;
import com.wewins.fota.bean.po.UpdatePlan;
import com.wewins.fota.common.constant.AuthConst;
import com.wewins.fota.service.PlanService;
import com.wewins.fota.sys.token.TokenManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/plan")
public class PlanController implements BasicController<UpdatePlan, PlanFilterCond> {
    private final PlanService service;

    private TokenManager tokenManager;

    @Autowired
    public PlanController(PlanService planService, TokenManager tokenManager) {
        this.service = planService;
        this.tokenManager = tokenManager;
    }

    @PostMapping("/query/info")
    @Override
    public ResponseRst<PageRst<UpdatePlan>> queryAll(@RequestBody PageReq<PlanFilterCond> pageReq) {
        return ResponseRst.<PageRst<UpdatePlan>>builder()
                .data(service.queryAll(pageReq))
                .build().withOk();
    }

    @PostMapping("/query/detail")
    public ResponseRst<PageRst<UpdatePlanDetails>> queryAllDetails(@RequestBody PageReq<PlanFilterCond> pageReq) {
        return ResponseRst.<PageRst<UpdatePlanDetails>>builder()
                .data(service.queryAllDetails(pageReq))
                .build().withOk();
    }

    @GetMapping("/query/info/{id}")
    @Override
    public ResponseRst<UpdatePlan> queryById(@PathVariable long id) {
        return ResponseRst.<UpdatePlan>builder()
                .data(service.findById(id))
                .build().withOk();
    }

    @GetMapping("/query/detail/{id}")
    public ResponseRst<UpdatePlanDetails> queryDetailById(@PathVariable long id) {
        return ResponseRst.<UpdatePlanDetails>builder()
                .data(service.findDetailById(id))
                .build().withOk();
    }

    @PostMapping("/insert")
    @Override
    public ResponseRst<UpdatePlan> insert(HttpServletRequest request, @Validated @RequestBody UpdatePlan plan) {
        return ResponseRst.<UpdatePlan>builder()
                .data(service.insert(this.tokenManager.getUsernameFromToken(request.getHeader(AuthConst.HTTP_HEADER_KEY_AUTHENTICATION)), plan))
                .build().withOk();
    }

    @DeleteMapping("/delete/{id}")
    @Override
    public ResponseRst<Integer> deleteById(HttpServletRequest request, @PathVariable long id) {
        return ResponseRst.<Integer>builder()
                .data(service.deleteById(this.tokenManager.getUsernameFromToken(request.getHeader(AuthConst.HTTP_HEADER_KEY_AUTHENTICATION)), id))
                .build().withOk();
    }

    @DeleteMapping("/delete")
    public ResponseRst<Integer> delete(HttpServletRequest request, @RequestBody UpdatePlan plan) {
        return ResponseRst.<Integer>builder()
                .data(service.delete(this.tokenManager.getUsernameFromToken(request.getHeader(AuthConst.HTTP_HEADER_KEY_AUTHENTICATION)), plan))
                .build().withOk();
    }

    @PutMapping("/update")
    @Override
    public ResponseRst<Integer> update(HttpServletRequest request, @RequestBody UpdatePlan plan) {
        return ResponseRst.<Integer>builder()
                .data(service.update(this.tokenManager.getUsernameFromToken(request.getHeader(AuthConst.HTTP_HEADER_KEY_AUTHENTICATION)), plan))
                .build().withOk();
    }

    @GetMapping("/query/name/{subName}")
    public ResponseRst<List<String>> queryPlanNames(@PathVariable String subName) {
        return ResponseRst.<List<String>>builder()
                .data(service.queryNames(subName))
                .build().withOk();
    }

    @GetMapping("/exist/{planName}")
    public ResponseRst<Boolean> existPlanByName(@PathVariable String planName) {
        return ResponseRst.<Boolean>builder()
                .data(service.existPlanByName(planName))
                .build().withOk();
    }
}
